package com.dingup.provider.util;

import com.dingup.api.model.sys.SysUser;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by wancheng on 2018/7/19.
 */
public class ClassUtil {

    public static List<String> getClassAttribute(Class<?> obj) {
        List<String> attributes = new ArrayList<>();
        Field[] fields = obj.getDeclaredFields();
        Arrays.stream(fields).forEach(
                field -> attributes.add(field.getName())
        );
        return attributes;
    }

    public static Object setAttribute(Object obj, String arrtibute, Object value) {
        System.out.println(value.getClass());
        Field[] fields = obj.getClass().getDeclaredFields();
        Arrays.stream(fields).filter(
                field -> field.getName().equals(arrtibute)
        ).forEach(
                field -> {
                    field.setAccessible(true);

                    try {
                        Class<?> typeclass = field.getType();
                        if (typeclass.equals(Integer.class)){
                            field.set(obj, Integer.valueOf(String.valueOf(value)));
                        }else if (typeclass.equals(String.class)){
                            field.set(obj, String.valueOf(value));
                        }
                        else if (typeclass.equals(Double.class)){
                            field.set(obj, Double.valueOf(String.valueOf(value)));
                        }
                        else if (typeclass.equals(Float.class)){
                            field.set(obj, Float.valueOf(String.valueOf(value)));
                        }
                        else if (typeclass.equals(Byte.class)){
                            field.set(obj, Byte.valueOf(String.valueOf(value)));
                        }
                        else if (typeclass.equals(Date.class)){

                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
        );
        return obj;
    }

    public static void main(String[] args) {
        SysUser user = new SysUser();
        setAttribute(user, "id", "1");
        System.out.print(user.getUserId());
    }
}
